﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace DVT.Admin.WinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }

        private string globalnewsid = null;
        private void btntijiao_Click(object sender, EventArgs e)
        {
            //if (!CheckTextBox())
            //{
            //    MessageBox.Show("标题或内容不能为空");
            //    return;
            //}
            if (globalnewsid == null)
            {
                //新增
                using (Model.NewsDBEntities entities = new NewsDBEntities())
                {
                    try
                    {
                        int istop = chktop.Checked == true ? 1 : 0;
                        int ismost = chkmost.Checked == true ? 1 : 0;
                        entities.News.AddObject(new News
                                                    {
                                                        Author = " 大旅招生办",
                                                        Id = Guid.NewGuid().ToString(),
                                                        IsTop = istop,
                                                        ModifyDate = DateTime.Now,
                                                        PostDate = DateTime.Now,
                                                        NewsContent = htmlEditor1.HTML.Trim(),
                                                        Title = txttitle.Text,
                                                        IsMost=ismost
                                                    });
                        entities.SaveChanges();
                        MessageBox.Show("Successfully");
                        ResetControl();
                    }
                    catch (Exception exception)
                    {

                        MessageBox.Show(exception.Message);
                    }


                }
            }
            //编辑
            else
            {

                using (Model.NewsDBEntities entities = new NewsDBEntities())
                {
                    try
                    {
                        int istop = chktop.Checked == true ? 1 : 0;
                        int ismost = chkmost.Checked == true ? 1 : 0;
                        News globalnews = entities.News.Where(c => c.Id == globalnewsid).FirstOrDefault();
                        if (globalnews != null)
                        {
                            globalnews.IsTop = istop;
                            globalnews.ModifyDate = DateTime.Now;
                            globalnews.Title = txttitle.Text;
                            globalnews.NewsContent = htmlEditor1.HTML;
                            globalnews.IsMost = ismost;
                            
                            entities.SaveChanges();
                            MessageBox.Show("Successfully");
                            ResetControl();
                        }
                    }
                    catch (Exception exception)
                    {

                        MessageBox.Show(exception.Message);
                    }


                }
            }



        }

        private void BindEditNewsData()
        {
            using (Model.NewsDBEntities entities = new NewsDBEntities())
            {
                dataGridView1.AutoGenerateColumns = false;
                var news= entities.News.AsEnumerable() ;
                dataGridView1.DataSource = news;
                


            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindEditNewsData();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //删除
            if(e.ColumnIndex==2)
            {
              if( MessageBox.Show("确定要删除吗？","删除",MessageBoxButtons.OKCancel)==DialogResult.OK)
              {
                  try
                  {

                      using (Model.NewsDBEntities entities = new NewsDBEntities())
                      {
                          string nid = (dataGridView1.CurrentRow.DataBoundItem as News).Id;
                          if (dataGridView1.CurrentRow != null)
                          {
                             // News  newinstance = dataGridView1.CurrentRow.DataBoundItem as News;
                              News newinstance = entities.News.Where(c => c.Id ==nid ).FirstOrDefault();
                        //  entities.News.DeleteObject(newinstance);
                          entities.DeleteObject(newinstance);
                              entities.SaveChanges();
                          MessageBox.Show("successfully");
                          BindEditNewsData();
                         }
                      }

                  }
                  catch (Exception ex)
                  {

                      MessageBox.Show(ex.Message);
                  }

              }
            }

           //编辑
            if (e.ColumnIndex==1)
            {
                News editNewsInstance = dataGridView1.CurrentRow.DataBoundItem as News;
                if(editNewsInstance!=null)
                {
                    globalnewsid = editNewsInstance.Id;
                    txttitle.Text = editNewsInstance.Title;
                    htmlEditor1.HTML = editNewsInstance.NewsContent;
                    chktop.Checked = editNewsInstance.IsTop == 1 ? true : false;
                    chkmost.Checked = editNewsInstance.IsMost == 1 ? true : false;
                   tabControl1.SelectTab(0);
                }
            }
        }

        private  bool CheckTextBox()
        {
            if (txttitle.Text.Trim() != "" && htmlEditor1.Text.Trim() != "")
                return true;
            else
            {
                return false;
            }
        }
        private void btncancel_Click(object sender, EventArgs e)
        {
            ResetControl();
        }

        private void ResetControl()
        {
            globalnewsid = null;
            txttitle.Text = "";
            htmlEditor1.HTML = "";
            chkmost.Checked = false;
            chktop.Checked = false;
        }
        private void btnrefresh_Click(object sender, EventArgs e)
        {
            BindEditNewsData();
        }
    }
}
